<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>项目管理系统</title>
<link rel="stylesheet" rev="stylesheet" href="../css/style.css" type="text/css" media="all" />
<script src="../../js/jquery1.9.js"></script>
<script src="../../js/jquery.form.js"></script>
<script src="../../js/ajaxfileupload.js"></script>
<script src="../../js/urlParams.js"></script>
<script charset="utf-8" src="../../js/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="../../js/kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="../../js/kindeditor/style/complex.js"></script>
<style type="text/css">
<!--
.atten {font-size:12px;font-weight:normal;color:#F00;}
-->
</style>
</head>

<body class="ContentBody">
<form id="form_id">
<div class="MainDiv">
<table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
      <th class="tablestyle_title" >产品编辑页面</th>
      <input type="hidden" name="size" id="products_size"/>
      <input type="hidden" name="id" id="products_id"/>
  </tr>
  <tr>
    <td class="CPanel">
		
		<table border="0" cellpadding="0" cellspacing="0" style="width:100%">
		<TR>
			<TD width="100%">
				<fieldset style="height:100%;">
				<legend>编辑产品</legend>
					  <table border="0" cellpadding="2" cellspacing="1" style="width:100%">
					 
					    <tr>
						    <td nowrap align="right" width="13%">名称:</td>
						    <td width="41%"><input id="products_name" name="name" class="text" style="width:250px" type="text" size="40" />
					        <span class="red"> *</span></td>
					    </tr>
					    
					    <tr>
						    <td nowrap align="right" width="13%">图片:</td>
						    <td width="38%">
						    	<input id="product_pic_id" name="uploadFile" class="text" style="width:290px" type="file" size="35" />
						    	<a  href="#"id="upload_btn">上传</a>
					        </td>
					    </tr>
					    
					    <tr>
						    <td nowrap align="right" width="13%">&nbsp;</td>
						    <td width="38%" id="del_tr" style="display: none">
						    	<img src="" alt="" id="pic_name_id" width="80" height="80"/>
						    	<a  href="#"id="del_btn">&nbsp;&nbsp;删除</a>
					        </td>
					    </tr>
					    
					   <tr>
						    <td nowrap align="right" height="120px">特点:</td>
						    <td colspan="3">
				    			<textarea id="products_feature" name="content" style="width:700px;height:200px;visibility:hidden;"></textarea>
						    </td>
					   </tr>
					  </table>
					</fieldset>			
				</TD>
		</TR>
		</TABLE>
	 </td>
  </tr>
		<TR>
			<TD colspan="2" align="center" height="50px">
			<input type="button" id="save_input" name="Submit" value="修改" class=""/>　
			
			<input id="backList_id" type="button" name="Submit2" value="返回列表" class=""/></TD>
		</TR>
		</TABLE>
	
	
	 </td>
  </tr>
  </table>

</div>
</form>

<script>
	$(function(){
		jQuery.ajax( {  
			          type : 'get',  
			          contentType : 'application/json',  
			          url : '../../products/get.do',  
			          dataType : 'json',  
			          data:"id="+$.request.queryString["id"],
			          success : function(obj) {  
		          		$("#products_id").val(obj.products.id);
		          		$("#products_size").val(obj.products.size);
		          		$("#products_name").val(obj.products.name);
		          		$("#product_pic_id").attr("pic_name",obj.products.picName);
		          		
		          		editor.html(obj.products.feature);
		          		
		          		$('#product_pic_id').attr("disabled",true);
						$('#upload_btn').hide();
		          		$('#del_tr').show();
   						$('#pic_name_id').attr("src","../../upload/download.do?fileName="+obj.products.picName);

			          },  
			          error : function(data) {  
			            alert("error")  
			          }  
		         }); 
	});
	
	function ajaxFileUpload() {
		if($('#product_pic_id').val()==""){
			alert("请先选中您要上传的图片");
			return;
		}
		$.ajaxFileUpload( {
			url : '../../upload/add.do', //你处理上传文件的服务端
			secureuri : false,
			fileElementId : 'product_pic_id',
			dataType : 'json',
			success : function(data) {
				$('#del_tr').css("display","block");
				$('#pic_name_id').attr("src","../../upload/download.do?fileName="+data.name);
				$('#products_size').val(data.size);
				$('#product_pic_id').attr("disabled",true);
				$('#product_pic_id').attr("pic_name",data.name);
				$('#upload_btn').hide();
			},
			error:function(){
				alert('error');
			}
		})

		return false;
	}
	
	$('#del_btn').click(function(){
		$('#del_tr').css("display","none");
		$('#product_pic_id').attr("disabled",false);
		$('#upload_btn').show();
	});
	$('#save_input').click(function() {
		var btnObj = $(this);

		if ($('#products_name').val() == '') {
			alert('请输入名称');
			return;
		}else if(!$('#product_pic_id').attr("disabled")){
			alert('请先上传图片');
			return;
		} else {
			$(this).attr('disabled', true);
			$(this).val('修改中...');
		}
		var content = editor.html();
		jQuery.ajax( {
			type : 'POST',
			url : '../../products/edit.do',
			dataType : 'json',
			data : {id:$('#products_id').val(),name:$('#products_name').val(),feature:content,picName:$('#product_pic_id').attr("pic_name"),size:$('#products_size').val()},
			success : function(obj) {
				btnObj.val('修改成功');
				alert('修改成功!');
				location.href = "listproducts.htm?currPage=1";
			},
			error : function() {
				alert('error');
			}
		});
	});
	
	$("#upload_btn").click(ajaxFileUpload);

	$('#backList_id').click(function() {
		location.href = "listproducts.htm?currPage=1";
	});
</script>
</body>
</html>
